import EasyXML
from EasyDebugger import *
import crumbs
import stdtags

from pygui import *
from ezpygui import *

DEBUG_PUZZLE = getDebugFlag()
addLevel(DEBUG_PUZZLE)

def register(parser, window):
    font = pygame.font.Font(None, 32)
    
    def nodeText(self, node):
        x = int(node.x)
        y = int(node.y)
        text = self.parseText(node._parent, node._textblock())
        print text
        
        if hasattr(node, "font"):
            font2 = pygame.font.Font(None, int(node.font))
        else:
            font2 = font
        w = wtext("bit" + str(self.puzzle.num), text, pygame.Rect(x,y,0,0), font2, (0,0,0))
        self.puzzle.num += 1
        self.puzzle.add(w)
    
        return (1, [])
	
    def nodeImage(self, node):
	x = int(node.x)
	y = int(node.y)
	img = self.parseText(node._parent, node.img)
	w = wimage("img" + str(self.puzzle.num), img, pygame.Rect(x, y, 0, 0))
	self.puzzle.num += 1
	self.puzzle.add(w)
	return (1, [])
    
    def nodeAnswerList(self, node):
        self.puzzle.answerlists.append(node)
        node.answered = False
        node.answers = []
        node.correct_parsed = self.parseText(node._parent, node.correct)
        
        def answerPressed(window, pos, buttons):
            window.answerlist.answered = window.id
            for item in window.answerlist.answers:
                item.setImage(node.img)
            window.setImage(node.img2)
            
        if hasattr(node, "font"):
            font2 = pygame.font.Font(None, int(node.font))
        else:
            font2 = font
        
        for answer in node._children:
            x = int(answer.x)
            y = int(answer.y)
            if(hasattr(node, "images")):
                w = wimagelabelbutton("bit" + str(self.puzzle.num), node.img, self.parseText(node._parent, answer._textblock()), font=font2, rect=pygame.Rect(x,y,0,0))
            else:
               w = wtxtbutton("bit" + str(self.puzzle.num), node.img, self.parseText(node._parent, answer._textblock()), font=font2, rect=pygame.Rect(x,y,0,0))
            w.image.answerlist = node
            w.image.id = answer.id
            w.image.function = answerPressed
            w.image.answer = answer
            node.answers.append(w.image)
            
            self.puzzle.num += 1
            self.puzzle.add(w)
            
        return (1, [])
        
        
    parser.registerFunc("text", nodeText)
    parser.registerFunc("image", nodeImage);
    parser.registerFunc("answerlist", nodeAnswerList)
    
    
        